﻿@using Website.CRUDServices
@using Website.Models.Styling
@using Location = Website.Models.Domain.Location
@model Website.Models.ViewModels.ListView<Cluster>
@{
    this.ViewBag.Title = "Klynger";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    var locations = this.ViewData["Locations"] as List<Location> ?? new List<Location>();
    DefaultSearchFields defaultFields = this.ViewData["DefaultFields"] as DefaultSearchFields ?? new DefaultSearchFields();
    var criteriaLocation = this.ViewData["CriteriaLocation"] as Guid?;
    var criteriaMethodTypes = this.ViewData["CriteriaMethodTypes"] as List<Guid> ?? new List<Guid>();
    var criteriaStatusTypes = this.ViewData["CriteriaStatusTypes"] as List<Guid> ?? new List<Guid>();
    List<ClusterMethodType> clusterMethodTypes = this.ViewData["ClusterMethodTypes"] as List<ClusterMethodType> ?? new List<ClusterMethodType>();
    List<ClusterStatusType> clusterStatusTypes = this.ViewData["ClusterStatusTypes"] as List<ClusterStatusType> ?? new List<ClusterStatusType>();
    var methods = this.ViewData["ClusterMethods"] as List<ClusterMethod>;
    var statuses = this.ViewData["ClusterStatuses"] as List<ClusterStatus>;
    
}
<h2>Søgeresultat - @ViewBag.Title</h2>

<fieldset class="searchCriterias">
    <legend>Søgning</legend>
    <table>    
        <tr>
            <td>Type:</td><td>@ViewBag.Title</td>
        </tr>
        <tr><td>Antal resultater i alt: </td><td>@Model.TotalRows</td></tr>
        @if (criteriaLocation.HasValue && criteriaLocation.Value != Guid.Empty)
        {
            <tr><td>Lokation: </td><td>@locations.Single(x => x.ID == criteriaLocation.Value).Name</td></tr>
        }
        @if (criteriaMethodTypes.Any())
        {
            <tr><td>Metoder og tematiserede rapporter:</td>
                <td>
                    @foreach (ClusterMethodType obj in clusterMethodTypes.Where(x => criteriaMethodTypes.Contains(x.ID)).OrderBy(x => x.Name))
                    {
                        @obj.Name
                        <br />
                    }
                </td>
            </tr>
        }
        
        @if (criteriaStatusTypes.Any())
        {
            <tr><td>Status typer:</td>
                <td>
                    @foreach (ClusterStatusType obj in clusterStatusTypes.Where(x => criteriaStatusTypes.Contains(x.ID)).OrderBy(x => x.Name))
                    {
                        @obj.Name
                        <br />
                    }
                </td>
            </tr>
        }
        @Html.Partial("~/Views/Reports/_SearchDefaultFieldsParametersTableTR.cshtml", defaultFields)

    </table>
</fieldset>


<table class="tableView tableWithOnlyLastChild">
    <tr>
        @foreach (TableViewHeaderItem item in this.Model.Header.Items)
        {
            <td>
                @Html.Partial("~/Views/_Shared/TableViewHeader.cshtml", item)
            </td>
        }
        <td></td>
    </tr>
      @if (this.Model != null)
    {
        foreach (Cluster cluster in this.Model.List)
        {
            var thisMethodTypes = methods.Where(x => cluster.Methods.Contains(x.ID)).Select(x => x.Type).Where(x => x != Guid.Empty);
            var thisStatusTypes = statuses.Where(x => cluster.Statuses.Contains(x.ID) && x.IsActive).Select(x => x.Type).Where(x => x != Guid.Empty);
   
        <tr>
            <td>@string.Format("{0:dd-MM-yyyy}", cluster.Date)</td>
       
            <td>@cluster.Name</td>
            <td>
                @if (thisStatusTypes.Any())
                {
                    @Html.Raw(string.Join(",<br/>", clusterStatusTypes.Where(x => thisStatusTypes.Contains(x.ID)).Select(x => x.Name)))
                }
            </td>
            <td>
                @if (thisMethodTypes.Any())
                {
                    @Html.Raw(string.Join(",<br/>", clusterMethodTypes.Where(x => thisMethodTypes.Contains(x.ID)).Select(x => x.Name)))
                }
            </td>
            <td><a href="@Url.Action("ViewCluster", "Cluster", new { Id = cluster.ID })" target="@cluster.ID.ToString()">
                        <img src="@Url.Content("~/Content/ButtonIcons/notepad_blue.png")" alt="Link"/></a></td>
        </tr>
        }
        <tr><td colspan="@(1+ Model.Header.Items.Count())"></td></tr>
    }
    </table>